/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.Event.EDIDataEvent;
	import com.edi.runtime.component.EDIApplication
	
	import mx.controls.Alert;
	import mx.core.Container;
	/**
	 * 数据源，主要负责数据的分发。 
	 * @author Administrator
	 * 
	 */	
	public class EDIDataSource 
	{
		private var _appName:String;
		private var _entity:Object;
		private var _id:String;
		private var _container:Container;
		private var _dataTableList:Array;
		private var _autoCommit:Boolean=false;
		
		/**
		 * 构造一个数据源 
		 * @param container 应用区域。通常指application.
		 * @param xml  数据源，即一张表、 
		 * @param appName 应用名
		 * 
		 */		
		public function EDIDataSource(container:Container,xml:XML=null,appName:String=null)
		{
			super();
			
			_entity=new Object();
			_dataTableList=new Array();
			_container=container;
			if(xml!=null)
				addDataTable(xml);
			if (appName!=null)
			{
				this._appName = appName;
			}
			
		}
		
		/**
		 * 自动提交， 
		 * @param value 
		 * 
		 */		
		public function set autoCommit(value:Boolean):void
		{
			_autoCommit=value;
			var i:int;
			for(i=0;i<_dataTableList.length;i++)
			{
				(_dataTableList[i] as EDIDataTable).autoCommit=value;
			}
		}
		public function get autoCommit():Boolean
		{
			return _autoCommit;
		}
		
		/**
		 * 将表的xml信息生成转换成EDIDataTable对象 
		 * @param tableXml 数据源，即一张表、 
		 * @param autoDispatch 自动分发。若分发将向container发送EDIDataEvent事件。
		 * 
		 */		
		public function addDataTable(tableXml:XML,autoDispatch:Boolean=false):void
		{
			var dsName:String=tableXml.dsName[0];
			if(_entity[dsName]!=null)
			{
				(_entity[dsName] as EDIDataTable).newRowState=tableXml.newRow;
				(_entity[dsName] as EDIDataTable).loadNewSource(XML(tableXml.table[0]).children());
			}
			else
			{
				var _dataTable:EDIDataTable=new EDIDataTable(XML(tableXml.table[0]).children(),this);
				_dataTable.newRowState=tableXml.newRow;
				_dataTable.dataSourceName=dsName;
				_dataTable.autoCommit=autoCommit;
				_entity[dsName]=_dataTable;
				_dataTableList.push(_dataTable);
			}
			if (autoDispatch){
				var ede:EDIDataEvent=new EDIDataEvent(EDIDataEvent.Data_Load);
				ede.value=_entity[dsName];
				ede.index=-1;
				ede.ediTarget=this;
				ede.tableName=dsName;			
				_container.dispatchEvent(ede);
			}
		}
		
		/**
		 * 添加一张空表 
		 * @param dataSourceName 表名
		 * @return EDIDataTable
		 * 
		 */		
		public function addNullDataTable(dataSourceName:String):EDIDataTable
		{
			var _dataTable:EDIDataTable=new EDIDataTable(null,this);
			_dataTable.dataSourceName=dataSourceName;
			_entity[dataSourceName]=_dataTable;
			_dataTableList.push(_dataTable);
			return _dataTable;
		}
		
		/**
		 * 根据表名获得该表的dataTable 
		 * @param tableName
		 * @return 
		 * 
		 */		
		public function getDataTable(tableName:String):EDIDataTable
		{
			return _entity[tableName];
		}
		
		/**
		 * 返回该dataSource内的所有dataTable 
		 * @return 
		 * 
		 */		
		public function dataTableList():Array
		{
			return _dataTableList;
		}
		
		/**
		 * 设置应用名。 
		 * @param value
		 * 
		 */		
		public function set appName(value:String):void
		{
			_appName=value;
		}
		
		public function get appName():String
		{
			return _appName==null?EDIApplication.appName:_appName;
		}
	}
}